﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DotNet.Edu.Entity;
using DotNet.Edu.Service;
using DotNet.EduWebApi.Models;
using DotNet.Extensions;
using DotNet.Utility;

namespace DotNet.EduWebApi.Controllers
{
    public class ExerciseController : BaseApiController
    {
        /// <summary>
        /// 获取顺序练习题
        /// </summary>
        /// <param name="userId">用户主键</param>
        [ApiCheck, HttpPost]
        public ActionResult GetSequenceQuest(string userId)
        {
            return QuestCore(userId, 1);
        }

        /// <summary>
        /// 获取随机练习题
        /// </summary>
        /// <param name="userId">用户主键</param>
        [ApiCheck, HttpPost]
        public ActionResult GetRandomQuest(string userId)
        {
            return QuestCore(userId, 2);
        }

        /// <summary>
        /// 获取模拟考试题
        /// </summary>
        /// <param name="userId">用户主键</param>
        [ApiCheck, HttpPost]
        public ActionResult GetSimulatorExamQuest(string userId)
        {
            #region 参数验证解密

            if (userId.IsEmpty())
            {
                return Json(new BoolMessage(false, "请指定学员主键"));
            }

            var validResult = EncryptUtil.Decrypt(userId, "学员主键");
            if (!validResult.Success)
            {
                return Json(validResult);
            }

            userId = validResult.Data;

            #endregion

            var student = GetStudentById(userId);
            var workCategoryId = student.WorkCategoryId;
            var data = EduService.Question.GetExamQuestions(workCategoryId,"");
            return Json(new ExamResultModel
            {
                Success = true,
                Message = null,
                Time = 60 * 60,
                Data = data
            });
        }

        /// <summary>
        /// 获取学员错题记录
        /// </summary>
        /// <param name="userId">用户主键</param>
        [ApiCheck, HttpPost]
        public ActionResult GetErrorQuest(string userId)
        {
            return QuestCore(userId, 4);
        }

        /// <summary>
        /// 获取学员收藏记录
        /// </summary>
        /// <param name="userId">用户主键</param>
        [ApiCheck, HttpPost]
        public ActionResult GetFavoriteQuest(string userId)
        {
            return QuestCore(userId, 5);
        }

        /// <summary>
        /// 删除学员错题记录
        /// </summary>
        /// <param name="userId">用户主键</param>
        /// <param name="questId">题目主键</param>
        [ApiCheck, HttpPost]
        public ActionResult DeleteErrorQuest(string userId, string questId)
        {
            return DeleteQuestCore(userId, questId, 1);
        }

        /// <summary>
        /// 删除学员收藏记录
        /// </summary>
        /// <param name="userId">用户主键</param>
        /// <param name="questId">题目主键</param>
        [ApiCheck, HttpPost]
        public ActionResult DeleteFavoriteQuest(string userId, string questId)
        {
            return DeleteQuestCore(userId, questId, 2);
        }

        /// <summary>
        /// 收藏题目
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="questId"></param>
        /// <param name="isFavorite"></param>
        /// <returns></returns>
        [ApiCheck, HttpPost]
        public ActionResult FavoriteQuest(string userId, string questId, string isFavorite)
        {
            #region 验证
            if (userId.IsEmpty())
            {
                return Json(new BoolMessage(false, "请指定学员主键"));
            }
            if (questId.IsEmpty())
            {
                return Json(new BoolMessage(false, "请指定题目主键"));
            }
            if (isFavorite.IsEmpty())
            {
                return Json(new BoolMessage(false, "请指定isFavorite"));
            }
            #endregion

            #region 解密
            var validResult = EncryptUtil.Decrypt(userId, "学员主键");
            if (!validResult.Success)
            {
                return Json(validResult);
            }
            userId = validResult.Data;

            validResult = EncryptUtil.Decrypt(questId, "题目主键");
            if (!validResult.Success)
            {
                return Json(validResult);
            }
            questId = validResult.Data;

            validResult = EncryptUtil.Decrypt(isFavorite, "isFavorite");
            if (!validResult.Success)
            {
                return Json(validResult);
            }
            var _isFavorite = validResult.Data.ToBool();
            #endregion

            var result = _isFavorite ?
                EduService.QuestionFavorite.Create(userId, questId, 2) :
                EduService.QuestionFavorite.Delete(userId, questId, 2);
            return Json(result);
        }

        /// <summary>
        /// 添加错题
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="questId"></param>
        /// <returns></returns>
        [ApiCheck, HttpPost]
        public ActionResult AddErrorQuest(string userId, string questId)
        {
            #region 验证
            if (userId.IsEmpty())
            {
                return Json(new BoolMessage(false, "请指定学员主键"));
            }
            if (questId.IsEmpty())
            {
                return Json(new BoolMessage(false, "请指定题目主键"));
            }
            #endregion

            #region 解密
            var validResult = EncryptUtil.Decrypt(userId, "学员主键");
            if (!validResult.Success)
            {
                return Json(validResult);
            }
            userId = validResult.Data;

            validResult = EncryptUtil.Decrypt(questId, "题目主键");
            if (!validResult.Success)
            {
                return Json(validResult);
            }
            questId = validResult.Data;
            #endregion

            var result = EduService.QuestionFavorite.CreateError(userId, questId);
            return Json(result);
        }
        
        private ActionResult DeleteQuestCore(string userId, string questId, int favType)
        {
            #region 验证
            if (userId.IsEmpty())
            {
                return Json(new BoolMessage(false, "请指定学员主键"));
            }
            if (questId.IsEmpty())
            {
                return Json(new BoolMessage(false, "请指定题目主键"));
            }
            #endregion

            #region 解密
            var validResult = EncryptUtil.Decrypt(userId, "学员主键");
            if (!validResult.Success)
            {
                return Json(validResult);
            }
            userId = validResult.Data;

            validResult = EncryptUtil.Decrypt(questId, "题目主键");
            if (!validResult.Success)
            {
                return Json(validResult);
            }
            questId = validResult.Data;
            #endregion

            var result = EduService.QuestionFavorite.Delete(userId, questId, favType);
            return Json(result);
        }

        private ActionResult QuestCore(string userId, int exerciseType)
        {
            #region 参数验证解密

            if (userId.IsEmpty())
            {
                return Json(new BoolMessage(false, "请指定学员主键"));
            }

            var validResult = EncryptUtil.Decrypt(userId, "学员主键");
            if (!validResult.Success)
            {
                return Json(validResult);
            }

            userId = validResult.Data;

            #endregion

            var student = GetStudentById(userId);
            var workCategoryId = student.WorkCategoryId;
            List<ExcerciseQuestion> data = null;
            if (exerciseType == 1)//顺序练习
            {
                data = EduService.Question.GetSeqQuestions(workCategoryId,"");
            }
            else if (exerciseType == 2)//随机练习
            {
                data = EduService.Question.GetRandomQuestions(workCategoryId, "");
            }
            else if (exerciseType == 3)//模拟考试
            {
                data = EduService.Question.GetExamQuestions(workCategoryId, "");
            }
            else if (exerciseType == 4)//错题记录
            {
                data = EduService.QuestionFavorite.GetFavoriteQuestions(userId, 1);
            }
            else if (exerciseType == 5)//收藏记录
            {
                data = EduService.QuestionFavorite.GetFavoriteQuestions(userId, 2);
            }
            return Json(new BoolMessageData(data));
        }
    }
}